package cn.iocoder.yudao.framework.business.basic.dao.query;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;

import java.util.Collection;

/**
 * 拓展 MyBatis Plus QueryWrapper 类
 *
 *  @param <T> 数据类型
 */
public class CoreLambdaQueryWrapper<T> extends LambdaQueryWrapperX<T> {

    @Override
    public CoreLambdaQueryWrapper<T> likeIfPresent(SFunction<T, ?> column, String val) {
        return (CoreLambdaQueryWrapper<T>) super.likeIfPresent(column, val);
    }

    @Override
    public CoreLambdaQueryWrapper<T> inIfPresent(SFunction<T, ?> column, Collection<?> values) {
        return (CoreLambdaQueryWrapper<T>) super.inIfPresent(column, values);
    }

    @Override
    public CoreLambdaQueryWrapper<T> inIfPresent(SFunction<T, ?> column, Object... values) {
        return (CoreLambdaQueryWrapper<T>) super.inIfPresent(column, values);
    }

    @Override
    public CoreLambdaQueryWrapper<T> eqIfPresent(SFunction<T, ?> column, Object val) {
        return (CoreLambdaQueryWrapper<T>) super.eqIfPresent(column, val);
    }

    @Override
    public CoreLambdaQueryWrapper<T> neIfPresent(SFunction<T, ?> column, Object val) {
        return (CoreLambdaQueryWrapper<T>) super.neIfPresent(column, val);
    }

    @Override
    public CoreLambdaQueryWrapper<T> gtIfPresent(SFunction<T, ?> column, Object val) {
        return (CoreLambdaQueryWrapper<T>) super.gtIfPresent(column, val);
    }

    @Override
    public CoreLambdaQueryWrapper<T> geIfPresent(SFunction<T, ?> column, Object val) {
        return (CoreLambdaQueryWrapper<T>) super.geIfPresent(column, val);
    }

    @Override
    public CoreLambdaQueryWrapper<T> ltIfPresent(SFunction<T, ?> column, Object val) {
        return (CoreLambdaQueryWrapper<T>) super.ltIfPresent(column, val);
    }

    @Override
    public CoreLambdaQueryWrapper<T> leIfPresent(SFunction<T, ?> column, Object val) {
        return (CoreLambdaQueryWrapper<T>) super.leIfPresent(column, val);
    }

    @Override
    public CoreLambdaQueryWrapper<T> betweenIfPresent(SFunction<T, ?> column, Object val1, Object val2) {
        return (CoreLambdaQueryWrapper<T>) super.betweenIfPresent(column, val1, val2);
    }

    @Override
    public CoreLambdaQueryWrapper<T> betweenIfPresent(SFunction<T, ?> column, Object[] values) {
        return (CoreLambdaQueryWrapper<T>) super.betweenIfPresent(column, values);
    }

    @Override
    public CoreLambdaQueryWrapper<T> eq(boolean condition, SFunction<T, ?> column, Object val) {
        return (CoreLambdaQueryWrapper<T>) super.eq(condition, column, val);
    }

    @Override
    public CoreLambdaQueryWrapper<T> eq(SFunction<T, ?> column, Object val) {
        return (CoreLambdaQueryWrapper<T>) super.eq(column, val);
    }

    @Override
    public CoreLambdaQueryWrapper<T> orderByDesc(SFunction<T, ?> column) {
        return (CoreLambdaQueryWrapper<T>) super.orderByDesc(column);
    }

    @Override
    public CoreLambdaQueryWrapper<T> last(String lastSql) {
        return (CoreLambdaQueryWrapper<T>) super.last(lastSql);
    }

    @Override
    public CoreLambdaQueryWrapper<T> in(SFunction<T, ?> column, Collection<?> coll) {
        return (CoreLambdaQueryWrapper<T>) super.in(column, coll);
    }

    /**
     * 设置只返回最后 N 条
     *
     * @return this
     */
    @SuppressWarnings("UnusedReturnValue")
    public CoreLambdaQueryWrapper<T> limitN(int n) {
        DbType dbType = JdbcUtils.getDbType();
        switch (dbType) {
            case ORACLE:
            case ORACLE_12C:
                throw new UnsupportedOperationException("Oracle 是通过 ROWNUM <= " + n + " 实现限制 " + n + " 条，待实现");
            case SQL_SERVER:
            case SQL_SERVER2005:
                throw new UnsupportedOperationException("SQL Server 是通过 SELECT TOP " + n + " 实现限制 " + n + " 条，待实现");
            default: // MySQL、PostgreSQL、DM 达梦、KingbaseES 大金都是采用 LIMIT 实现
                super.last("LIMIT " + n);
        }
        return this;
    }

}
